﻿using System;
using System.Windows.Forms;
using ClassLib.Classes.Common;
using ISumen;
using Sumen.Business;
using Sumen.Business.Model;
using Sumen.Business.Model.Presenter.Sys;

namespace Sumen_Stock.LookAndFeel.Admin
{
    public partial class Frm_User_Edit : DevExpress.XtraEditors.XtraForm
    {

        #region Property
        private UserPresenter _userPresenter;
        private SysRolePresenter _sysRolePresenter;
        private FromCode _fromCode = FromCode.FrmUser;
        private EnumFormStatus _enumFormStatus;
        private user _user;
        public Sumen.Business.Model.user User
        {
            get { return _user; }
            set { _user = value; }
        }
        public ISumen.EnumFormStatus EnumFormStatus
        {
            get { return _enumFormStatus; }
            set
            {
                _enumFormStatus = value;
                if (_enumFormStatus == EnumFormStatus.Add)
                {
                    buttonsArray1.btnSua.Enabled = false;
                    buttonsArray1.btnThem.Enabled = true;
                }
                else if (_enumFormStatus == EnumFormStatus.Modify)
                {
                    buttonsArray1.btnSua.Enabled = true;
                    buttonsArray1.btnThem.Enabled = false;
                }
            }
        }
        #endregion

        /// <summary>
        /// ///////////////////////////////////////////////////////////////////////////////////////////////////
        /// </summary>

        #region Form

        public Frm_User_Edit()
        {
            InitializeComponent();
            _userPresenter = new UserPresenter();
            _sysRolePresenter = new SysRolePresenter();
        }

        private void Frm_User_Edit_Load(object sender, EventArgs e)
        {
            LoadForm();
        }

        #endregion

        /// <summary>
        /// ///////////////////////////////////////////////////////////////////////////////////////////////////
        /// </summary>

        #region Button event

        private void buttonsArray1_btnEventAdd_click(object sender, EventArgs e)
        {
            if (Validation())
            {
                if (_enumFormStatus == EnumFormStatus.Add)
                {
                    //add
                    user newUser = addUser();
                    if (_userPresenter.Add(newUser) > 0)
                    {
                        ClsMsgBox.ThongTin("Thêm mới nhân viên thành công");
                        ClsPublic.WriteLog(_fromCode, EnumFormStatus.Add, newUser.User_Username, "");
                        User = newUser;
                        this.EnumFormStatus = EnumFormStatus.Modify;
                    }
                }
            }
        }

        private void buttonsArray1_btnEventUpdate_click(object sender, EventArgs e)
        {
            if (Validation())
            {
                if (_enumFormStatus == EnumFormStatus.Modify)
                {
                    user newUser = addUser();
                    if (_userPresenter.Update(newUser, newUser.User_Username) > 0)
                    {
                        ClsMsgBox.ThongTin("Cập nhật nhân viên thành công");
                    }
                    ClsPublic.WriteLog(_fromCode, EnumFormStatus.Modify, newUser.User_Username, "");
                }
            }
        }

        private void buttonsArray1_btnEventClose_click(object sender, EventArgs e)
        {
            this.Hide();
        }

        #endregion

        /// <summary>
        /// ///////////////////////////////////////////////////////////////////////////////////////////////////
        /// </summary>


        #region MEthod

        void LoadForm()
        {
            if (_enumFormStatus == EnumFormStatus.Add)
            {
                txtUsername.EditValue = null;
                txtUsername.Properties.ReadOnly = false;
                txtUsername.EditValue = null;
                txtFullName.EditValue = null;
                txtPhone.EditValue = null;
                txtAddress.EditValue = null;
                txtContact.EditValue = null;
                txtEmail.EditValue = null;
                chkActived.CheckState = CheckState.Checked;
            }
            else if (_enumFormStatus == EnumFormStatus.Modify)
            {
                txtUsername.EditValue = _user.User_Username;
                txtUsername.Properties.ReadOnly = true;
                txtPass.EditValue = null;
                txtFullName.EditValue = _user.User_FullName;
                txtPhone.EditValue = _user.User_Phone;
                txtAddress.EditValue = _user.User_Address;
                txtContact.EditValue = _user.User_Contact;
                txtEmail.EditValue = _user.User_Email;
                chkActived.Checked = _user.User_Actived ?? false;
            }
        }

        bool Validation()
        {
            dxErrorProvider1.ClearErrors();
            if (txtUsername.EditValue == null || txtUsername.EditValue == "")
            {
                dxErrorProvider1.SetError(txtUsername, "Vui lòng nhập tài khoản");
                return false;
            }
            if (_enumFormStatus == EnumFormStatus.Add)
            {
                if (txtPass.EditValue == null || txtPass.EditValue == "")
                {
                    dxErrorProvider1.SetError(txtPass, "Vui lòng nhập mật khẩu");
                    return false;
                }
            }
            if (txtFullName.EditValue == null || txtFullName.EditValue == "")
            {
                dxErrorProvider1.SetError(txtFullName, "Vui lòng nhập họ tên");
                return false;
            }

            return true;
        }

        user addUser()
        {
            user newUser = new user();
            newUser.User_Username = txtUsername.EditValue + string.Empty;
            newUser.User_Password = txtPass.EditValue + string.Empty;
            newUser.User_FullName = txtFullName.EditValue + string.Empty;
            newUser.User_Phone = txtPhone.EditValue + string.Empty;
            newUser.User_Address = txtAddress.EditValue + string.Empty;
            newUser.User_Contact = txtContact.EditValue + string.Empty;
            newUser.User_Email = txtEmail.EditValue + string.Empty;
            newUser.User_Actived = chkActived.CheckState == CheckState.Checked ? true : false;
            newUser.User_CreationDate = DateTime.Today;
            return newUser;
        }

        #endregion





    }
}